if (!window.jQuery) {
    throw new Error("必须提前引入jquery.js");
}
// 60秒倒计时
$.fn.countDown = function () {
    var num = 60,
        timer,
        that = $(this);
    that.on('click', function () {
        count(that);
        that.off('click');
    })
    function count(obj) {
        if (num == 0) {
            
            obj.attr('disabled', false);
            obj.text('获取验证码');
            num = 60;
            clearTimeout(timer);
            obj.on('click', function () {
                timer = setTimeout(function () {
                    count(obj)
                }, 1000);
                obj.off('click');
            })
            return;
        } else {
            obj.attr('disabled', true);
            obj.text(num + 's后重新获取');
            num--;
        }
        timer = setTimeout(function () {
            count(obj)
        }, 1000)
    }
}

/*
    *参数说明: Img:图片ID;Width:预览宽度;Height:预览高度;ImgType:支持文件类型;Callback:选择文件显示图片后回调方法;
    *使用方法:
    <div>
    <img id="ImgPr" width="120" height="120" /></div>
    <input type="file" id="up" />
    把需要进行预览的IMG标签外 套一个DIV 然后给上传控件ID给予uploadPreview事件
    $("#up").uploadPreview({ Img: "ImgPr", Width: 120, Height: 120, ImgType: ["gif", "jpeg", "jpg", "bmp", "png"], Callback: function () { }});
    */
//单图上传
jQuery.fn.extend({
    uploadPreview: function (opts) {
        var _self = this,
            _this = $(this);
        opts = jQuery.extend({
            Img: 'ImgPr',
            Width: 100,
            Height: 100,
            ImgType: ['gif', 'jpeg', 'jpg', 'bmp', 'png'],
            Callback: function () {
            }
        }, opts || {});
        _self.getObjectURL = function (file) {
            var url = null;
            if (window.createObjectURL != undefined) {
                url = window.createObjectURL(file)
            } else if (window.URL != undefined) {
                url = window.URL.createObjectURL(file)
            } else if (window.webkitURL != undefined) {
                url = window.webkitURL.createObjectURL(file)
            }
            return url
        };
        _this.change(function () {
            if (this.value) {
                if (!RegExp("\.(" + opts.ImgType.join("|") + ")$", "i").test(this.value.toLowerCase())) {
                    alert("选择文件错误,图片类型必须是" + opts.ImgType.json(',') + "中的一种");
                    this.value = '';
                    return false;
                }
                if ($.support.msie) { // jQuery 从 1.9 版开始，移除了 $.browser 和 $.browser.version ， 取而代之的是 $.support
                    try {
                        $("#" + opts.Img).attr('src', _self.getObjectURL(this.files[0]))
                    } catch (e) {
                        var src = "";
                        var obj = $("#" + opts.Img);
                        var div = obj.parent("div")[0];
                        _self.select();
                        if (top != self) {
                            window.parent.document.body.focus()
                        } else {
                            _self.blur()
                        }
                        src = document.selection.createRange().text;
                        document.selection.empty();
                        obj.hide();
                        obj.parent("div").css({
                            'filter': 'progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)',
                            'width': opts.Width + 'px',
                            'height': opts.Height + 'px'
                        });
                        div.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = src
                    }
                } else {
                    $("#" + opts.Img).attr('src', _self.getObjectURL(this.files[0]))
                }
                opts.Callback()
            }
        })
    }
});

/*
 使用单图上传
 img必须使用div为父级元素
  $("input[type='file']").uploadPreview({ Img: "id名", Width: 120, Height: 120 });
  $("input[type='file']").uploadPreview({ Img: "file", Width: 120, Height: 120 });
 */



 // js封装方法
 //封装insertAfter  功能类似insertBefore
    Element.prototype.insertAfter = function (targetNode, afterNode) {
        var beforeNode = afterNode.nextElementSibling;
        if (beforeNode == null) {
            this.appendChild(targetNode);
        } else {
            this.insertBefore(targetNode, beforeNode);
        }
    }

    //文档的高度  宽度  (封装getScrollHeight()函数)
function getScrollHeight(){
	　　if(document.body.scrollWidth){
		return {
			w:document.body.scrollWidth,
			h:document.body.scrollHeight
		}
	}else {
		return {
			w: document.documentElement.scrollWidth,
			h: document.documentElement.scrollHeight
		}
	}
}

// 浏览器可视区窗口宽高 不包括工具栏和滚动条
function getViewportOffset(){
	if(window.innerWidth){
		return {
			x:window.innerWidth,
			h:window.innerHeight
		}
	}else {
		if(document.compatMode=="BackCompat"){
			//document.compatMode=="BackCompat" //向后兼容
			return {
				w:document.body.clientWidth,
				h:document.body.clientHeight
			}
		}else {
			return {
				w:document.documentElement.clientWidth,
				h:document.documentElement.clientHeight
			}
		}
	}
}

 // 查看滚动条
 function getScrollOffset() {
 	if(window.pageXOffset){
 		return {
 			x:window.pageXOffset,
 			y:window.pageYOffset
 		}
 	}else{
 		return {
 			x:document.body.scrollLeft+document.documentElement.scrollLeft,
 			y:document.body.scrollTop+document.documentElement.scrollTop
 		}
 	}
 } 

  // 获取URL拼接参数
  function GetRequest() {
        var url = location.search; //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            strs = str.split("&");
            for(var i = 0; i < strs.length; i ++) {
                theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }
    var Request = new Object();
    Request = GetRequest();
    // 
    // 参数值 = Request['参数1'];